import Header from '../../../components/Header.vue'
import persons from "../../../pages/persons";
import mediaVue from "../../media";
import device from "../../../pages/device";
import deviceAll from "../../../pages/device/all";
import videoConference from "../../../pages/videoConference"

export default {
	name: 'company',
	components: {
		Header,
		persons,
		mediaVue,
		device,
		deviceAll,
		videoConference,
	},
	data() {
		return {
			activeName: 'person',
			data4: 'hello',
			name: 'shengwm',
			flag: false,
			treedata: [],
			showdialog: false,
			saving: false,
			dataImageList: '',
			addForm: {
				id: null,
				title: '',
				resolution: ''
			},
			addRules: {
				title: [{
						required: true,
						message: this.$t('message.required'),
						trigger: 'blur'
					},
					{
						max: 20,
						message: this.$t('message.length20'),
						trigger: 'blur'
					},
					{
						pattern: /^([\u4e00-\u9FFFa-zA-Z0-9+\s]+)$/,
						message: this.$t('message.incorrectFormat'),
						trigger: 'blur'
					}
				],
				contact: [{
						required: true,
						message: this.$t('message.required'),
						trigger: 'blur'
					},
					{
						max: 20,
						message: this.$t('message.length20'),
						trigger: 'blur'
					},
				],
				phoneno: [{
						required: true,
						message: this.$t('message.required'),
						trigger: 'blur'
					},
					{
						max: 20,
						message: this.$t('message.length20'),
						trigger: 'blur'
					},
					{
						pattern: /(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}/,
						message: this.$t('message.incorrectFormat'),
						trigger: 'blur'
					}
				],
				email: [{
						required: true,
						message: this.$t('message.required'),
						trigger: 'blur'
					},
					{
						max: 50,
						message: this.$t('message.length50'),
						trigger: 'blur'
					},
					{
						type: 'email',
						message: this.$t('message.mailFormat'),
						trigger: 'blur'
					}
				],
				resolution: [{
					required: true,
					message: this.$t('message.required'),
					trigger: 'blur'
				}, ],
				totalDuration: [{
						required: true,
						message: this.$t('message.required'),
						trigger: 'blur'
					},
					{
						max: 20,
						message: this.$t('message.length20'),
						trigger: 'blur'
					},
					{
						pattern: /^[0-9]+([.]{1}[0-9]+){0,1}$/,
						message: this.$t('message.incorrectFormat'),
						trigger: 'blur'
					}
				]
			},
			face: {
				name: '',
				group: '',
				file: ''
			},
			defaultProps: {
				children: 'children',
				label: 'text',
				id: 'id'
			},
			fileList: [],
			groupName: '',
			showdialogroot: false,

			curpid: 0,
			showMain: false,
			groupid: 0,
			titleForm: '',
			//resolutionList: [],
			user: {},
			resolutionTypes: [],
			node: {}
		}
	},
	mounted() {
		this.showGroupList();
		this.user = JSON.parse(localStorage.getItem('userInfo'));
	},
	methods: {
		tabClick(tab) {
			if (tab.name === 'media') {
				this.$refs.media.getCompanyId();
				this.$refs.media.query(1);
			} else if (tab.name === 'person') {
				this.$refs.persons.getCompanyId();
				this.$refs.persons.query(1);
			} else if (tab.name === 'device') {
				this.$refs.device.getCompanyId();
				this.$refs.device.query(1);
			} else if (tab.name === 'videoConference') {
				this.$refs.videoConference.getCompanyId();
				this.$refs.videoConference.query(1);
			}
		},
		addNode(node, data) {

			if (node.level >= 5) {
				this.$message.error(this.$t('message.bottom'));
				return;
			}

			this.titleForm = this.$t('company.newGroup');
			this.curpid = data.id;
			this.showdialogroot = true;
			this.flag = true;
			if (this.$refs['addForm']) {
				this.$refs['addForm'].resetFields();
			}
			this.addForm.id = null;
			this.addForm.title = '';
			this.addForm.contact = '';
			this.addForm.phoneno = '';
			this.addForm.email = '';
			this.addForm.resolution = '';
			this.addForm.totalDuration = '';

			var resolutionTypeList = this.$getDictList("device_resolution");
			var resolutionTypes1 = [];
			resolutionTypeList.forEach(function(e) {
				if (parseInt(e.value) <= parseInt(data.data.resolution)) {
					resolutionTypes1.push(e);
				}
			});
			this.resolutionTypes = resolutionTypes1;
		},
		delNode(node, data) {
			this.$confirm(this.$t('message.DDGroup'), this.$t('standard.prompt'), {
				confirmButtonText: this.$t('standard.confirm'),
				cancelButtonText: this.$t('standard.cancel'),
				type: 'warning'
			}).then(() => {
				this.axios.hPost('/api/company/delete', {
					id: data.id
				}, (response) => {
					this.showMain = false;
					if (response.success) {
						const parent = node.parent;
						const children = parent.data.children || parent.data;
						const index = children.findIndex(d => d.id === data.id);
						children.splice(index, 1);
						this.$message.success(response.msg);
						this.showGroupList();
					} else {
						this.$message.error(response.msg);
					}
				});
			});
		},
		editNode(node, data) {
			this.titleForm = this.$t('company.modifyGroup');
			this.showdialogroot = true;
			this.addForm.id = data.id;
			this.addForm.resolution = data.data.resolution;
			this.addForm.totalDuration = data.data.totalDuration + "";
			this.flag = false;
			this.node = node;

			var resolutionTypeList = this.$getDictList("device_resolution");
			var resolutionTypes1 = [];
			if (!node.parent.parent) {
				this.resolutionTypes = resolutionTypeList;
			} else {
				resolutionTypeList.forEach(function(e) {
					if (parseInt(e.value) <= parseInt(node.parent.data.data.resolution)) {
						resolutionTypes1.push(e);
					}
				});
				this.resolutionTypes = resolutionTypes1;
			}

		},
		shownode(node) {
			this.groupName = node.text;
			this.showMain = true;
			this.groupid = node.id;
			this.activeName = "device";
			localStorage.setItem("companyId", node.id);
			localStorage.setItem("company", JSON.stringify(node.data));

			this.$refs.device.getCompanyId();
			this.$refs.device.query(1);
		},
		handleAddTop() {
			this.titleForm = this.$t('company.newGroup');
			this.showdialogroot = true;
			this.flag = true;
			if (this.$refs['addForm']) {
				this.$refs['addForm'].resetFields();
			}
			this.addForm.id = null;
			this.addForm.title = '';
			this.addForm.contact = '';
			this.addForm.phoneno = '';
			this.addForm.email = '';
			this.addForm.resolution = '';
			this.addForm.totalDuration = '';
			this.curpid = 0;
			this.resolutionTypes = this.$getDictList("device_resolution");
		},
		addGroup() {
			if (!!this.node.childNodes) {
				if (!!this.addForm.id) {
					var flag = false;
					this.node.childNodes.find(item => {
						if (item.data.data.resolution > this.addForm.resolution) {
							this.$message.error(this.$t('message.resolvingPower'));
							flag = true;
							return;
						}
					});
					if (flag) {
						return;
					}
				}
			}
			let url = '/api/company/insert';
			if (!!this.addForm.id) {
				url = '/api/company/update';
			}
			this.saving = true;
			this.$refs['addForm'].validate((valid) => {
				if (valid) {
					let params = {
						id: this.addForm.id,
						companyName: this.addForm.title,
						companyExtraInfo: {
							contact: this.addForm.contact,
							phoneno: this.addForm.phoneno,
							email: this.addForm.email
						},
						resolution: this.addForm.resolution,
						totalDuration: this.addForm.totalDuration,
						parentId: this.curpid
					};
					this.axios.hPost(url, params, (response) => {
						if (response.success) {
							this.$message.success(response.msg);
							this.showdialogroot = false;
							this.showGroupList();
						} else {
							this.$message.error(response.msg);
						}
						this.saving = false;
					});
				} else {
					this.saving = false;
					return false;
				}
			});
		},
		showGroupList() {
			this.axios.hPost("/api/company", {}, (data) => {
				if (data.data) {
					this.treedata = data.data;
					this.shownode(data.data[0]);
				}
			});
		},
	},
	watch: {
		showMain(val) {
			if (!val) {
				this.$refs.deviceAll.findCompanyList();
			}
		}
	}
}
